<?php
namespace Libraries;
class CDate {
    public static function get_week() {
        return date("Y-m-l", strtotime('this week', time()));
    }
    public static function x_week_range($date) {
        $ts = \DateTime::createFromFormat('Y-m-d', $date)->getTimestamp();
        $start = (date('w', $ts) == 0) ? $ts : strtotime('last sunday', $ts);
        return array('start' => date('Y-m-d', $start),
            'end' => date('Y-m-d', strtotime('next saturday', $start)));
    }
    public static function create_week_period($start, $end) {
        if (is_null($end)) {
            $newdate = new \DateTime();
            $end = $newdate->format('Y-m-d');
        }
        $weeks = array();
        $ret = CDate::x_week_range($start);
        $ret['start'] = $start;
        if (CDate::get_date_diff_in_day($ret['end'], $end) <= 0)
                $ret['end'] = $end;
        $weeks[] = $ret;
        while (CDate::get_date_diff_in_day($ret['end'], $end) > 0) {
            $now = \DateTime::createFromFormat('Y-m-d', $ret['end'])->getTimestamp();
            $ret = CDate::x_week_range(date('Y-m-d', strtotime('tomorrow', $now)));
            if (CDate::get_date_diff_in_day($ret['end'], $end) <= 0)
                    $ret['end'] = $end;
            $weeks[] = $ret;
        }
        return $weeks;
    }
    public static function get_date_diff_in_day($date1, $date2) {
        $datetime1 = \DateTime::createFromFormat('Y-m-d', $date1);
        $datetime2 = \DateTime::createFromFormat('Y-m-d', $date2);
        $interval = date_diff($datetime1, $datetime2);
        return (int) $interval->format('%R%a');
    }
    public static function period2date($month, $year) {
        $timestamp = \DateTime::createFromFormat('d/m/Y', "01/$month/$year");
        $start = $timestamp->format('Y-m-d');
        $end = $timestamp->format('Y-m-t');
        return array(
            'start' => $start,
            'end' => $end
        );
    }
    public static function sql2date($date, $return_pattern = 'd/m/Y') {
        $date = \DateTime::createFromFormat('Y-m-d', $date);
// $date->setTimeZone(new DateTimeZone('Asia/Jakarta'));
        return $date->format($return_pattern);
    }
    public static function date2sql($date, $input_pattern = 'd/m/Y') {
        $date = \DateTime::createFromFormat($input_pattern, $date);
        return $date->format('Y-m-d');
    }
    public static function get_date_tomorrow($format = 'Y-m-d H:i:s') {
        $date = new \DateTime();
        $date->modify("+1 day");
        return $date->format($format);
    }
    public static function get_time_now() {
        return CDate::Now('H:i:s');
    }
    public static function Now($format = 'Y-m-d H:i:s') {
        $date = new \DateTime();
        return $date->format($format);
    }
    public static function period2date_now() {
        $timestamp = new \DateTime();
        $start = $timestamp->format('Y-m-d');
        $end = $timestamp->format('Y-m-t');
        return array(
            'start' => $start,
            'end' => $end
        );
    }
}
?>